require "clamp"

class Bash < Clamp::Command
	option ["-v", "--version"], :flag, "Show version" do
	  require "wpb/version"
		puts "WordPress Bash #{WPB::VERSION} (c) James Birtles"
		exit(0)
  end
  
  self.default_subcommand = "console"
  
  subcommand "config", "Add a host to the config to easily connect" do
    parameter "NAME", "The configuration name (used as a reference later)"
    option "--host", "HOST", "The ip address of the database server", default: "127.0.0.1"
	  option "--port", "PORT", "The port number of the database server", default: "3306"
	  option "--db", "DB", "The wordpress database", default: "wordpress"
	  option "--user", "USER", "The username to connect to the database", default: "root"
	  option "--pass", "PASS", "The password to connect to the database", default: "root"
	  option "--ssh-user", "SSH_USER", "The username to connect to ssh (same as database username if not given)"
	  option "--ssh-pass", "SSH_PASS", "The password to connect to ssh (same as database password if not given)"
	  
    def execute
      require "yaml"
      config_dir = File.join(File.expand_path("~"), ".wpb")
      Dir.mkdir(config_dir) unless Dir.exists?(config_dir)
      hosts_file = File.join(config_dir, "hosts.yaml")
      config = {}
      config = YAML::load(File.read(hosts_file)) if File.exists?(hosts_file)
      config[name.downcase.to_sym] = {
        active_record: {
          host: host,
          port: port,
          username: user,
          password: pass,
          database: db
        },
        ssh: {
          user: ssh_user || user,
          pass: ssh_pass || pass
        }
      }
      File.open(hosts_file, "w") { |f| f.write(config.to_yaml) }
    end
  end
	
	subcommand ["c", "console"], "Start the wpb console" do
	  parameter "[NAME]", "The name of the configuration to load"
	  option "--no-ssh", :flag, "Use this if you are doing it locally or you don't have ssh access to your server"
	  option "--host", "HOST", "The ip address of the database server", default: "127.0.0.1"
	  option "--port", "PORT", "The port number of the database server", default: "3306"
	  option "--db", "DB", "The wordpress database", default: "wordpress"
	  option "--user", "USER", "The username to connect to the database", default: "root"
	  option "--pass", "PASS", "The password to connect to the database", default: "root"
	  option "--ssh-user", "SSH_USER", "The username to connect to ssh (same as database username if not given)"
	  option "--ssh-pass", "SSH_PASS", "The password to connect to ssh (same as database password if not given)"
	  
	  def execute
      options = {
        active_record: {
          host: host,
          port: port,
          username: user,
          password: pass,
          database: db
        },
        ssh: {
          user: ssh_user || user,
          pass: ssh_pass || pass
        }
      }
      
      if name
        msg = "You must create that configuration first!"
        hosts_file = File.join(File.expand_path("~"), ".wpb", "hosts.yaml")
        if File.exists? hosts_file
          config = YAML::load(File.read(hosts_file))
          if config[name.downcase.to_sym]
            options.merge! config[name.downcase.to_sym]
          else
            puts msg
          end
        else
          puts msg
        end
      end
	    
      require "pry"
      if no_ssh?
        require "wpb"
        WPB::App.run options[:active_record]
        WPB.pry
      else
        require 'net/ssh/gateway'
        gateway = Net::SSH::Gateway.new options[:active_record][:host], options[:ssh][:user], password: options[:ssh][:pass]
        if gateway.active?
          port = gateway.open('127.0.0.1', options[:active_record][:port], 3307)
          fork do
            require "wpb"
            WPB::App.run options[:active_record].merge(host: "127.0.0.1", port: 3307)
            WPB.pry
          end
          Process.wait
          gateway.shutdown!
        else
          puts "Error: Could not connect to ssh, make sure your details are correct or try using the --no-ssh flag"
          gateway.shutdown!
        end
      end
    end
	end
	
	subcommand "init", "Commands to be run when first using WPB with your wordpress instance" do
	  option "--no-ssh", :flag, "Use this if you are doing it locally or you don't have ssh access to your server"
	  option "--host", "HOST", "The ip address of the database server", default: "127.0.0.1"
	  option "--port", "PORT", "The port number of the database server", default: "3306"
	  option "--db", "DB", "The wordpress database", default: "wordpress"
	  option "--user", "USER", "The username to connect to the database", default: "root"
	  option "--pass", "PASS", "The password to connect to the database", default: "root"
	  option "--ssh-user", "SSH_USER", "The username to connect to ssh (same as database username if not given)"
	  option "--ssh-pass", "SSH_PASS", "The password to connect to ssh (same as database password if not given)"
	  
	  def execute
	    options = {
        active_record: {
          host: host,
          port: port,
          username: user,
          password: pass,
          database: db
        },
        ssh: {
          user: ssh_user || user,
          pass: ssh_pass || pass
        }
      }
	    
	    if no_ssh?
  	    require "wpb"
  	    WPB::App.connect options[:active_record]
  	    WPB::AddTypeToWpPosts.up
	    else
        require 'net/ssh/gateway'
        gateway = Net::SSH::Gateway.new options[:active_record][:host], options[:ssh][:user], password: options[:ssh][:pass]
        if gateway.active?
          port = gateway.open('127.0.0.1', options[:active_record][:port], 3307)
          fork do
      	    require "wpb"
      	    WPB::App.connect options[:active_record].merge(host: "127.0.0.1", port: 3307)
      	    WPB::AddTypeToWpPosts.up
          end
          Process.wait
          gateway.shutdown!
        else
          puts "Error: Could not connect to ssh, make sure your details are correct or try using the --no-ssh flag"
          gateway.shutdown!
        end
      end
    end
  end
end